<?php

class FW24hGridHelper {
	/**
	 * @var array $__header;
	 *
	 * @access private;
	 */
	var $__header = null;
	
	var $__editURL = "<a href='%s'>Edit</a>";
	var $__deleteURL = "&nbsp;&nbsp;<a href='%s'>Delete</a>";
	
	/**
	 * @var array $__content;
	 *
	 * @access private;
	 */
	var $__content = null;
	
	/**
	 * @var array $__key;
	 * 
	 * @access private.
	 */
	var $__keys = array();
	
	/**
     * set header data
     * 
     * @access public
     * @return GridHelper Object;
	 */
	function setHeader($header){
		$this->__header = $header;
		return $this;
	}
	
	/**
     * get header data;
     * 
	 */
	public function getHeader(){
		return $this->__header;
	}
	
	/**
	 * get header of  grid control.
	 * 
	 * @param array $options.
	 * @access protected
	 * @return string
	 */
	protected function _getGridHeader($options){
		if(!isset($this->__header)) return;
		$tring = '<tr>';
		foreach($this->__header as $key => $text){
			$string .='<th>';
			if( $text['headerType'] == 'checkbox') {
				$string .= '<input type="checkbox" name="check_all" id="cbxCheckAll">';
			}else{
				$string .= $text['Title'];
			}
			$string .='</th>';
			$this->__keys[] = $key;
		}
		$string .= '</tr>';
		
		return $string;
	}
	
	function _getGridBody( $gridData) {
		$string = '';
		foreach( $gridData as $data) {
			$string .= '<tr>';
			foreach( $this->__keys as $key) {
				$text = $data[$key];
				if( $function=$this->__header[$key]['function']) {
					$text = $this->$function( array( 'value'=>$data, 'name'=>$key));
				}
				$string .= '<td>'.$text.'</td>';
			}
			$string .= '</tr>';
		}
		return $string;
	}
	
	function display( $data) {
		$string = '<table width="100%" align="center" border="0" cellpadding="0" cellspacing="0">';
		$string .= $this->_getGridHeader(array());
		$string .= $this->_getGridBody( $data);
		$string .= '</table>';
		return $string;
	}
	function optionBox( $data) {
		$name = $data['name'];
		return "<input type='checkbox' name='{$name}[]' id='key_{$name}' value='{$data[value][$name]}'>";
	}
	
	function doAction( $data) {
		$name = $data['name'];
		$name = $this->__header[$name]['key'];
		$text = sprintf( $this->__editURL, $data['value'][$name]);
		$text .= sprintf( $this->__deleteURL, $data['value'][$name]);
		return $text;
	}
}
